package fr.efrei.tradesys.struts.actions.connection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;

import fr.efrei.tradesys.hibernate.beans.AppUser;
import fr.efrei.tradesys.hibernate.factory.DaoFactory;
import fr.efrei.tradesys.treatments.MailHandler;

public class GetPasswordBackAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static Log				LOGGER						= LogFactory
																									.getLog(GetPasswordBackAction.class);
	private String						username;

	public String execute() throws Exception
	{
		LOGGER.info("Execution of " + this.getClass().getName());
		if ((username == null) || (username.compareTo("") == 0))
		{
			addActionError("The \"Login\" field is empty.");
			return ERROR;
		}
		else
		{
			AppUser u = DaoFactory.getInstance().getAppUserDao().getAppUser(username);
			if (u == null)
			{
				addActionError("The login " + username + " doesn't exist.");
				return ERROR;
			}
			else
			{
				new MailHandler()
						.sendMail(
								u.getMail(),
								"Password given back",
								"Hi,\n\nYou reported that you forgot your password.\nThe password associated to your login "
										+ u.getLogin()
										+ " is : "
										+ u.getPassword()
										+ ".\nKeep this password safely in order to be able to use the Trading System tool.\n\nSincerely,\n\nChatech");
				addActionMessage("Your password was sent to you by e-mail.");
				return SUCCESS;
			}
		}
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

}
